package team

import (
	"data/config"
	"data/view"
	"data/view/common"
	"data/view/other"
	"data/view/user"
	"data/view/user_team"
	"public/message"
	"public/mysqldb"
	"strings"
)

//判断团队能否删除
func CheckTeamIsDelete(ref_id int) (b bool) {
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	var ut []view.User_team_tbl
	orm.Where("team_ref_id = ? ", ref_id).Find(&ut)
	if len(ut) == 0 {
		b = true
	}
	return
}

//判断区域是否正确
func CheckRegionIsRight(province, city, region string) (b bool) {
	b = true
	if len(province) == 0 && len(city) == 0 && len(region) == 0 {
		b = false
		return
	}
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	var team, team1, team2 []Team_info_view
	if len(region) > 0 {
		//省市区都有值
		orm.Where("province = ? and city = ? and region = ?", province, city, region).Find(&team)
		orm.Where("province = ? and city = ? and region = ?", province, city, "").Find(&team1)
		orm.Where("province = ? and city = ? and region = ?", province, "", "").Find(&team2)
	} else {
		if len(city) > 0 {
			//省区
			orm.Where("province = ? and city = ?", province, city).Find(&team)
			orm.Where("province = ? and city = ?", province, "").Find(&team1)
		} else {
			if len(province) > 0 {
				//省
				orm.Where("province = ?", province).Find(&team)
			}
			//else 三者为空
		}
	}
	if len(team) > 0 || len(team1) > 0 || len(team2) > 0 {
		b = false
	}
	return
}

//获取某一团队全部成员
func OnGetTeamMembers(ref_id int) (result []user.User_tbl) {
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	var ut []view.User_team_tbl
	orm.Where("team_ref_id = ? ", ref_id).Find(&ut)
	if len(ut) > 0 {
		for _, v := range ut {
			u, _ := user.GetUserDetail(v.User)
			result = append(result, u)
		}
	}
	return
}

//获取团队详情
//name为空则返回全部团队
func GetTeamDetail(name string) (result map[string]TeamInfoDetail) {
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	var tmp []Team_info_view
	if len(name) > 0 {
		orm.Where("name = ? ", name).Find(&tmp)
	} else {
		orm.Find(&tmp)
	}

	if len(tmp) > 0 {
		result = make(map[string]TeamInfoDetail)
		for _, v := range tmp {
			ex := result[v.Name]
			if ex.Id == 0 {
				var info TeamInfoDetail
				var tmp_ = strings.Split(v.Leader, common.SPLIT)
				var leaders, members []user.User_tbl
				if len(tmp_) > 0 {
					for _, j := range tmp_ {
						u, _ := user.GetUserDetail(j)
						leaders = append(leaders, u)
					}

				}
				members = OnGetTeamMembers(v.Id)
				info.Id = v.Id
				info.Name = v.Name
				info.Leader = leaders
				info.Is_vaild = v.Is_vaild
				info.Create_time = v.Create_time
				info.Region_info = []other.Region_config_tbl{other.Region_config_tbl{Province: v.Province,
					City: v.City, Region: v.Region}}
				info.Member = members

				result[v.Name] = info
			} else {

				ex.Region_info = append(ex.Region_info, other.Region_config_tbl{Province: v.Province,
					City: v.City, Region: v.Region})

				result[v.Name] = ex
			}

		}
	}

	return
}

//添加团队地区和成员
func OnAddTeamRegionAndMember(name string, member []string, team_ref_id int, region_info []other.Region_config_tbl) (mess message.MessageBody) {
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	mess = message.GetSuccessMsg()
	//添加地区
	if len(region_info) > 0 {
		for _, v := range region_info {
			tmp := other.OnGetRegion(v.Province, v.City, v.Region)
			var t_region view.Team_region_tbl
			t_region.Name = name
			t_region.Region_ref_id = tmp.Id

			if orm.Create(&t_region).Error != nil {
				mess = message.GetErrorMsg(message.UnknownError)
				return
			}
		}
	}
	//添加成员
	if len(member) > 0 {
		for _, v := range member {
			var tmp view.User_team_tbl
			tmp.User = v
			tmp.Team_ref_id = team_ref_id

			if orm.Create(&tmp).Error != nil {
				mess = message.GetErrorMsg(message.UnknownError)
				return
			}

		}
	}
	return
}

//更新团队地区和成员
func OnUpdateTeamRegionAndMember(name string, member []string, team_ref_id int, region_info []other.Region_config_tbl) (mess message.MessageBody) {
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	//删除地区
	orm.Table("team_region_tbl").Where("name = ? ", name).Delete(&view.Team_region_tbl{})
	//删除成员
	orm.Table("user_team_tbl").Where("team_ref_id = ? ", team_ref_id).Delete(&view.User_team_tbl{})

	return OnAddTeamRegionAndMember(name, member, team_ref_id, region_info)
}

//获取用户所在团队基本信息
func OnGetTeamBasicInfoByUser(username string) (result TeamInfoBasic) {
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	var basic view.Team_info_tbl

	var u_t view.User_team_tbl
	orm.Where("user = ? ", username).Find(&u_t)
	if u_t.Id > 0 {
		basic = user_team.OnGetTeamBasic("", u_t.Id)
	} else {
		orm.Where("leader like ? ", "%"+username+"%").Find(&basic)
	}

	if basic.Id > 0 {
		result.Id = basic.Id
		result.Create_time = basic.Create_time
		result.Is_vaild = basic.Is_vaild
		result.Name = basic.Name
		var leaders []user.User_tbl
		var tmp_ = strings.Split(basic.Leader, common.SPLIT)
		if len(tmp_) > 0 {
			for _, j := range tmp_ {
				u, _ := user.GetUserDetail(j)
				leaders = append(leaders, u)
			}

		}
		result.Leader = leaders
	}
	return
}
